package com.lagouedu.cfgcenter.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@RestController
public class TestController {

    @Autowired
    private DruidDataSource druidDataSource;

    public DruidDataSource getDruidDataSource() {
        return druidDataSource;
    }

    public void setDruidDataSource(DruidDataSource druidDataSource) {
        this.druidDataSource = druidDataSource;
    }

    @RequestMapping("/getResult")
    public String getMsgFromMysql() throws SQLException {
        DruidPooledConnection connection = druidDataSource.getConnection();
        String sql = "select user_name from user";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        String user_name = "";
        while (resultSet.next()){
            user_name = resultSet.getString("user_name");
        }
        return user_name;
    }

    @RequestMapping("/getResult1")
    public String getMsgFromMysql1() throws SQLException {
        DruidPooledConnection connection = druidDataSource.getConnection();
        String sql = "select menu_name from pait_menu_info where menu_id='10001'";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        String menu_name = "";
        while (resultSet.next()){
            menu_name = resultSet.getString("user_name");
        }
        return menu_name;
    }
}
